<?php
class Content_PageController extends Areal_Controller_Action
{
    public function getModel($type_alias){
        if ($type_alias == 'help') return new Content_Model_Help();
        if ($type_alias == 'article') return new Content_Model_Article();
        //  return new Content_Model_Page();
    }

    public function adminAction()
    {
        $type_alias = $this->getRequest()->getParam('type_alias');
        $model = $this->getModel($type_alias);
        $this->view->assign('pages', $model->getAll());
        $this->view->assign('type_alias', $type_alias);
    }

    public function listAction()
    {
        $type_alias = $this->getRequest()->getParam('type_alias');
        $model = $this->getModel($type_alias);
        $this->view->assign('pages', $model->getActivePageList());
        $this->view->assign('type_alias', $type_alias);
    }

    public function showAction()
    {
        $page_id = $this->getRequest()->getParam('page_id');
        $model = Doctrine::getTable('Content_Model_Page')->find($page_id);
        $this->view->page = $model;
    }

    public function addAction()
    {
        $type_alias = $this->getRequest()->getParam('type_alias');
        $model = $this->getModel($type_alias);
        if ($this->getRequest()->isPost()){
            if ($model->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Page saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','page','content',
                                                      array('type_alias'=> $type_alias));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Page not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $model->getEditingForm();
        $this->view->type_alias = $type_alias;
    }

    public function editAction()
    {
        $page_id = $this->getRequest()->getParam('page_id');
        $model = Doctrine::getTable('Content_Model_Page')->find($page_id);
        if ($this->getRequest()->isPost()){
            if ($model->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Page saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','page','content',
                                                  array('type_alias'=>$model['type']) );
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Page not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->getRequest()->setParam('type_alias', $model['type']);
        $this->view->form = $model->getEditingForm();
    }

    public function deleteAction()
    {
        $page_id = $this->getRequest()->getParam('page_id');
        $page = Doctrine::getTable('Content_Model_Page')->find($page_id);
        $form = new Default_Form_ConfirmForm();
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                $page->delete();
                $message = Zend_Registry::get('Zend_Translate')->translate('Page deleted');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','page','content',
                                                  array('type_alias'=>$page['type']) );
            }
        }
        $this->getRequest()->setParam('type_alias', $page['type']);
        $this->view->form = $form;
    }

    public function promoteAction()
    {
        $page_id = $this->getRequest()->getParam('page_id');
        $page = Doctrine::getTable('Content_Model_Page')->find($page_id);
        $page->promote();
        return $this->_helper->redirector('admin','page','content',
                                                  array('type_alias'=>$page['type']) );
    }

    public function demoteAction()
    {
        $page_id = $this->getRequest()->getParam('page_id');
        $page = Doctrine::getTable('Content_Model_Page')->find($page_id);
        $page->demote();
        return $this->_helper->redirector('admin','page','content',
                                                  array('type_alias'=>$page['type']) );
    }


}